package ioperation;

import Book.Book;
import Book.BookShelf;

import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookShelf bookShelf){
        //1.判满
        int count= bookShelf.getNum();
        if(count== bookShelf.getBooks().length){
            System.out.println("书架满了！无法添加图书！");
        }else {
            //2.添加书
            System.out.println("请输入书名：");
            Scanner scanner=new Scanner(System.in);
            String name=scanner.nextLine();
            System.out.println("请输入作者名：");
            String author=scanner.nextLine();
            System.out.println("请输入类型：");
            String type=scanner.nextLine();
            System.out.println("请输入价格：");
            int price=scanner.nextInt();
            Book newbook=new Book(name,author,price,type);
            //3.判断书架中是否有这本书
            for (int i = 0; i < count; i++) {
                Book book=bookShelf.getBook(i);
                if(book.getName().equals(name)){
                    System.out.println("书架中已经有这本书，不能重复放入！");
                    System.out.println(book);
                    return;
                }
            }
            //4.添加进书架
            System.out.println("新增图书中....");
            bookShelf.setBooks(count,newbook);
            bookShelf.setNum(count+1);
            System.out.println("新增图书成功！");

        }
    }
}
